home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / pc / files / t_unix / j109lxa4.tar / tcpdump.c < prev    next >
C/C++ Source or Header  |  1994-06-04  |  2KB  |  104 lines

  1. /* TCP header tracing routines
  2.  * Copyright 1991 Phil Karn, KA9Q
  3.  */
  4. #include <stdio.h>
  5. #include "global.h"
  6. #include "mbuf.h"
  7. #include "netuser.h"
  8. #include "internet.h"
  9. #include "tcp.h"
  10. #include "ip.h"
  11. #include "trace.h"
  12.  
  13. /* TCP segment header flags */
  14. static char *Tcpflags[] = {
  15.     "FIN",    /* 0x01 */
  16.     "SYN",    /* 0x02 */
  17.     "RST",    /* 0x04 */
  18.     "PSH",    /* 0x08 */
  19.     "ACK",    /* 0x10 */
  20.     "URG",    /* 0x20 */
  21.     "CE"    /* 0x40 */
  22. };
  23.  
  24. /* Dump a TCP segment header. Assumed to be in network byte order */
  25. void
  26. #ifdef MONITOR
  27. tcp_dump(fp,bpp,source,dest,check,mon)
  28. #else
  29. tcp_dump(fp,bpp,source,dest,check)
  30. #endif
  31. FILE *fp;
  32. struct mbuf **bpp;
  33. int32 source,dest;    /* IP source and dest addresses */
  34. int check;        /* 0 if checksum test is to be bypassed */
  35. #ifdef MONITOR
  36. int mon;
  37. #endif
  38. {
  39.     struct tcp seg;
  40.     struct pseudo_header ph;
  41.     int16 csum;
  42.     int16 dlen;
  43.  
  44.     if(bpp == NULLBUFP || *bpp == NULLBUF)
  45.         return;
  46.  
  47.     /* Verify checksum */
  48.     ph.source = source;
  49.     ph.dest = dest;
  50.     ph.protocol = TCP_PTCL;
  51.     ph.length = len_p(*bpp);
  52.     csum = cksum(&ph,*bpp,ph.length);
  53.  
  54.     ntohtcp(&seg,bpp);
  55.  
  56. #ifdef MONITOR
  57.     if (mon)
  58.         fprintf(fp, "%u->%u", seg.source, seg.dest);
  59.     else
  60. #endif
  61.     fprintf(fp,"TCP: %u->%u Seq x%lx",seg.source,seg.dest,seg.seq,seg.ack);
  62. #ifdef MONITOR
  63.     if (!mon)
  64. #endif
  65.     if(seg.flags.ack)
  66.         fprintf(fp," Ack x%lx",seg.ack);
  67.     if(seg.flags.congest)
  68.         fprintf(fp," %s",Tcpflags[6]);
  69.     if(seg.flags.urg)
  70.         fprintf(fp," %s",Tcpflags[5]);
  71.     if(seg.flags.ack)
  72.         fprintf(fp," %s",Tcpflags[4]);
  73.     if(seg.flags.psh)
  74.         fprintf(fp," %s",Tcpflags[3]);
  75.     if(seg.flags.rst)
  76.         fprintf(fp," %s",Tcpflags[2]);
  77.     if(seg.flags.syn)
  78.         fprintf(fp," %s",Tcpflags[1]);
  79.     if(seg.flags.fin)
  80.         fprintf(fp," %s",Tcpflags[0]);
  81.  
  82. #ifdef MONITOR
  83.     if (!mon)
  84. #endif
  85.     fprintf(fp," Wnd %u",seg.wnd);
  86.     if(seg.flags.urg)
  87.         fprintf(fp," UP x%x",seg.up);
  88.     /* Print options, if any */
  89. #ifdef MONITOR
  90.     if (!mon)
  91. #endif
  92.     if(seg.mss != 0)
  93.         fprintf(fp," MSS %u",seg.mss);
  94. #ifdef MONITOR
  95.     if (!mon)
  96. #endif
  97.     if((dlen = len_p(*bpp)) != 0)
  98.         fprintf(fp," Data %u",dlen);
  99.     if(check && csum != 0)
  100.         fprintf(fp," CHECKSUM ERROR (%u)",csum);
  101.     fprintf(fp,"\n");
  102. }
  103.  
  104.